<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\piles;

use app\common\dao\BaseDao;
use app\common\model\piles\PilesBasicModel;

class PilesBasicDao extends BaseDao
{

    /**
     * @return PilesBasicModel
     */
    protected function getModel(): string
    {
        return PilesBasicModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', intval($where['is_type']));
            })
            ->when(isset($where['plan_id']) && $where['plan_id'] !== '', function ($query) use ($where) {
                $query->where('plan_id', intval($where['plan_id']));
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', intval($where['is_show']));
            })
            ->when(isset($where['piles_type']) && $where['piles_type'] !== '', function ($query) use ($where) {
                $query->where('piles_type', intval($where['piles_type']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('piles_name|remark', '%' . trim($where['keywords']) . '%');
            });
    }

}
